import { getCaptcha } from '@/request/api';
import { Button, Checkbox, Form, Input } from 'antd';
import { Buffer } from 'buffer';
import React, { useEffect, useState } from 'react';
import { useNavigate } from 'react-router-dom';
import styles from './index.module.less';

const App: React.FC = () => {
  const navigate = useNavigate()
  const onFinish = (values: any) => {
    console.log('Success:', values);
    sessionStorage.setItem('username',values.username)
    navigate('/')
  };

  console.log(styles)

  const onFinishFailed = (errorInfo: any) => {
    console.log('Failed:', errorInfo);
  };

  const [remarkSvg,setRemarkSvg] = useState('')

  const changeCapchat = ()=>{
    getCaptcha().then((res:{data:string})=>{
      const svg = res.data
      let base64 = Buffer.from(svg).toString('base64')
      let imgData = 'data:image/svg+xml;charset=utf-8;base64,'+base64;
      setRemarkSvg(imgData)
    })
  }

  useEffect(()=>{
    changeCapchat()
  },[])

  return (
    <div className={styles['login-wrap']}>
    <Form
      name="basic"
      labelCol={{ span: 8 }}
      wrapperCol={{ span: 16 }}
      initialValues={{ remember: true }}
      onFinish={onFinish}
      onFinishFailed={onFinishFailed}
      autoComplete="off"
      className={styles.login}
    >
      <Form.Item
        label="Username"
        name="username"
        rules={[{ required: true, message: 'Please input your username!' }]}
      >
        <Input />
      </Form.Item>

      <Form.Item
        label="Password"
        name="password"
        rules={[{ required: true, message: 'Please input your password!' }]}
      >
        <Input.Password />
      </Form.Item>
      <Form.Item
        label="验证码11"
        name="remark"
        rules={[{ required: true, message: 'Please input your password!' }]}
      >
        <div style={{display:'flex'}}>
        <Input />
        <img onClick={changeCapchat} src={remarkSvg} alt="" style={{width:'96px',height:'32px'}} />
        </div>
      </Form.Item>

      <Form.Item name="remember" valuePropName="checked" wrapperCol={{ offset: 8, span: 16 }}>
        <Checkbox>Remember me</Checkbox>
      </Form.Item>

      <Form.Item wrapperCol={{ offset: 8, span: 16 }}>
        <Button type="primary" htmlType="submit">
          Submit
        </Button>
      </Form.Item>
    </Form>
    </div>
  );
};

export default App;